package editor.graphics;

import spark.graphics.*;
import spark.exception.LoadingException;

import javax.swing.*;
import javax.imageio.ImageIO;
import java.util.HashMap;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.io.File;
import java.io.IOException;

import editor.logic.LevelSpriteFactory;
import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.XMLReaderFactory;

public class SpriteMapPanel extends JPanel {
	private JScrollPane sb;
	private int chartwidth = 107;

	public SpriteMapPanel(File spriteMap, String themePath, String theme, BrushSelectionListener brushSelectionListener, SpritePanelFactory spritePanelFactory) throws LoadingException {
		super();
		sb = new JScrollPane(this, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		try {
			HashMap<String, SpriteMap.SpriteData> spriteDataMap = new HashMap<String, SpriteMap.SpriteData>();
			File[] themeFile = new File[1];
			getSpriteMapData(spriteMap, themePath, theme, spriteDataMap, themeFile);
			BufferedImage image = ImageIO.read(themeFile[0]);
			String[] mapNamePieces = spriteMap.getAbsolutePath().split("[/\\\\\\\\]");
			setName(mapNamePieces[mapNamePieces.length - 1] + ":" + theme);
			if(!LevelSpriteFactory.levelSpriteFactoryHandler.containsFactory(spriteMap+":"+theme)) {
				new LevelSpriteFactory(spriteMap.getPath(), themePath, theme, spriteDataMap);
			}
			for(java.util.Map.Entry<String, SpriteMap.SpriteData> entry : spriteDataMap.entrySet()) {
				Image scaledImage = image.getSubimage((int) entry.getValue().uv.x, (int) entry.getValue().uv.y, (int) entry.getValue().dimensions.x, (int) entry.getValue().dimensions.y);
				scaledImage = scaledImage.getScaledInstance(Window.ICON_DIMENSION, Window.ICON_DIMENSION, Image.SCALE_SMOOTH);
				add(
						spritePanelFactory.makeSpritePanel(
								spriteMap.getPath(), theme, entry.getKey(),
								new ImageIcon(scaledImage),
								brushSelectionListener));
			}
		}
		catch(IOException e) {
			throw new LoadingException(e);
		}
	}
	private void getSpriteMapData(File spriteMap, String themePath, String theme, HashMap<String, SpriteMap.SpriteData> spriteDataMap, File[] themeFile) throws LoadingException {
		try {
			XMLReader reader = XMLReaderFactory.createXMLReader();
			SpriteMapParser parser = new SpriteMapParser(theme, spriteDataMap);
			reader.setContentHandler(parser);
			reader.parse(spriteMap.getAbsolutePath());
			themeFile[0] = new File(themePath + parser.getThemeFileName());
			if(!themeFile[0].exists()) {
				throw new LoadingException("Theme file of theme " + theme + "(" + themeFile[0].getAbsolutePath() + ") does not exist");
			}
		}
		catch(SAXException e) {
			throw new LoadingException(e);
		}
		catch(IOException e) {
			throw new LoadingException(e);
		}
	}


	public JScrollPane getScrollPane() {
		return sb;
	}

	@Override
	public Dimension getPreferredSize() {
		Dimension dim = new Dimension();
		dim.width = (int) sb.getViewport().getSize().getWidth();
		int col = dim.width / chartwidth;
		int row = (int) (Math.ceil(24.0 / col));
		dim.height = row * chartwidth;
		return dim;
//		int col, row, h, w;
//		Dimension d = null;
//		w = (int) sb.getViewport().getSize().getWidth();
//		col = w / chartwidth;
//		if(col > 0) {
//			row = (int) (Math.ceil(24.0 / col));
//			h = row * chartwidth;
//			d = new Dimension(w, h);
//		}
//		else {
//			d = super.getPreferredSize();
//		}
//		return d;
	}
}
